<?php
class GoogleAPI{
	
	public function __construct() {
		
	}
	
    public function lookupFeed($query_url) {
        
        $url = "https://ajax.googleapis.com/ajax/services/feed/lookup?"."v=1.0&q=".urlencode($query_url);    
		$url = $this->addRequiredParams($url);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        
        $body = curl_exec($ch);
        curl_close($ch);
    
        
        $json = json_decode($body);
        
        return $json;
    }

	
	public function getTopSearchKeywords(){
		$url = "http://www.google.com/m/services/trends/get?hl=zh-TW&gl=tw&category=web&nocache=".time();
		$url = $this->addRequiredParams($url);
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		
		$result = curl_exec($ch);
		//echo "<pre>";print_r($result);echo "</pre>";
		curl_close($ch);
		$doc = new DOMDocument();
				
		$doc->strictErrorChecking = FALSE;		
		$doc->loadXML($result);
		$xml = simplexml_import_dom($doc);
		
		//echo "<pre>";print_r($xml);echo "</pre>";
		return $xml->item;
	}
	
	public function getExpandWords($word_array){
		
		$expand_words = array();
		
		foreach($word_array as $key=>$search_term){			
			$ch = curl_init();
			$expand_url = 'http://clients1.google.com.tw/complete/search?hl=zh-TW'; 
			$expand_url = $this->addRequiredParams($expand_url);
			if(isset($word_array[$key+1])){
				$params = '&q='.$word_array[$key].$word_array[$key+1];
				//exactly match
				//$expand_words[] = $word_array[$key].$word_array[$key+1];
				//array_push($expand_words,$word_array[$key].$word_array[$key+1]);
			}
			else{
				$params = '&q='.$search_term;	
				//exactly match
				//$expand_words[] = $search_term;
				//array_push($expand_words,$search_term);			
			}
			
			curl_setopt($ch, CURLOPT_URL,$expand_url.$params);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);			
			$result = curl_exec($ch);
			curl_close($ch);		
			$json = substr($result, 19,-1);
			$json = mb_convert_encoding($json, "UTF-8", "big5");
			$json_decoded = json_decode($json);
			//$json_decoded[1][0][0] 是第一個字
			if(isset($json_decoded[1][0][0]) && isset($json_decoded[1][1][0]) && isset($json_decoded[1][2][0]) ){
				array_push($expand_words,$json_decoded[1][0][0],$json_decoded[1][1][0],$json_decoded[1][2][0]);
                /*
                 * 設定最多顯示15個延伸字
                 */
                if (count($expand_words) >= 14) {
                    break;
                }
			}	
		}
		return array_unique($expand_words);
	}
	
    public function getBlogResults($start='',$q) {
        $google_blog_search = 'http://ajax.googleapis.com/ajax/services/search/blogs?hl=zh-TW&ie=UTF-8&v=1.0&rsz=large';
		
		$google_blog_search = $this->addRequiredParams($google_blog_search);
		
        if ($start=='') {
            
            $start = '0';
            $url = $google_blog_search."&q=".urlencode($q)."&start=".$start;
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $body = curl_exec($ch);
            curl_close($ch);
            $json = json_decode($body);
        } else {
            $url = $google_blog_search."&q=".urlencode($q)."&start=".$start;
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $body = curl_exec($ch);
            curl_close($ch);
            $json = json_decode($body);
        }
		
    	return $json;
    }
	
	private function addRequiredParams($url){
		$USER_IP_ADDR = $_SERVER['REMOTE_ADDR'];
        $url .= '&key='.GOOGLE_API_KEY.'&userip='.$USER_IP_ADDR;
		
		return $url;
	}
	
}


?>